package com.ke.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.ke.dao.ArticleOrderRepository;
import com.ke.pojo.ArticleOrder;
import com.ke.pojo.BaseResp;
import com.ke.service.ArticleOrderService;
import com.ke.utils.ArticlePayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@CrossOrigin(allowCredentials = "true")
@RestController
@RequestMapping("/articleOrder")
public class ArticleOrderController {

    @Autowired
    ArticleOrderService articleOrderService;


    /**
     * 从购物车发起支付功能
     * @param map
     * @return
     */
    @RequestMapping("/addArticleOrder")
    public String addArticleOrder(@RequestBody Map map, HttpServletRequest request){
        List<Integer> ids = (List)map.get("ids");
        return articleOrderService.addArticleOrder(ids,request);
    }

    /**
     * 从订单页面发起支付
     * @param map
     * @param request
     * @return
     */
    @RequestMapping("/addArticleOrderFromOrder")
    public String addArticleOrderFromOrder(@RequestBody Map map, HttpServletRequest request){
        Integer id = (Integer) map.get("id");
        return articleOrderService.addArticleOrderFromOrder(id,request);
    }


    @RequestMapping("/returnMessage")
    public void returnMessage(HttpServletRequest request) throws AlipayApiException {
        Map<String, String> stringStringMap = ArticlePayUtil.convertRequestParamsToMap(request);
        boolean  signVerified = AlipaySignature.rsaCheckV1(stringStringMap, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgZKfe4FGRbBhNCXnssSVsWwmXIdYZ30PO+dc4AlQal7XORt5O40E2Zai+TQxfvzwOfxg6UCGviDEQR677yEnOfAgigNBs6czalf3lEB8MsMbOv5lQ1OkYqtKhCNjFBIreL7ml5XFHHBrtWV8gTxAJXVWTRjB8xxa0h209ACfEnL6Aw3phN1r3G+AX5Z2s4JYZzKSmvHwmFziyxJZp6QmBZE1/gJlGvhhWk8/kQD9Rnp+i8O5IgOlVxt7LHcsuU/uK2qS6rICIylirp9MSNov2UGi8jfNjkB9uLKIy4Q9Wcdi4GLiEONRu+GCmrFGtXt2ruTzf7NA+g9re3Adv9yHwwIDAQAB", "utf-8", "RSA2");  //调用SDK验证签名
        if (signVerified){
            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商户自身业务处理，校验失败返回failure
            System.out.println(stringStringMap);

            String orderId = stringStringMap.get("out_trade_no");
            String trade_status = stringStringMap.get("trade_status");
            articleOrderService.updateOrderStatus(orderId,trade_status);


        } else {
            // TODO 验签失败则记录异常日志，并在response中返回failure.
        }
    }

    @RequestMapping("/findAllArticleOrder")
    public BaseResp findAllArticleOrder(HttpServletRequest request){
        return articleOrderService.findAllArticleOrder(request);
    }

    @RequestMapping("/deleteArticleOrder")
    public BaseResp deleteArticleOrder(@RequestBody Map map , HttpServletRequest request){
        return articleOrderService.deleteArticleOrder(map,request);
    }


}
